package other.xsy.pms.order;

import com.rkhd.platform.sdk.data.model.Order;
import com.rkhd.platform.sdk.exception.ScriptBusinessException;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.model.XObject;
import com.rkhd.platform.sdk.trigger.DataResult;
import com.rkhd.platform.sdk.trigger.Trigger;
import com.rkhd.platform.sdk.trigger.TriggerRequest;
import com.rkhd.platform.sdk.trigger.TriggerResponse;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @Description: 订单审核通过推送PMS
 * @Author: 黎炎松
 * @Date: 2024/12/12 15:24
 */
public class TriggerPushPms implements Trigger {

    private static final Logger LOGGER = LoggerFactory.getLogger();

    /**
     * 修改触发事件
     * @param request
     * @return
     * @throws ScriptBusinessException
     */
    @Override
    public TriggerResponse execute(TriggerRequest request) throws ScriptBusinessException {
        LOGGER.info("订单归档时，推送订单数据到PMS 触发开始");
        List<XObject> dataList = request.getDataList();
        List<DataResult> dataResultList = new ArrayList<>();
        List<Order> orderList = new ArrayList<>();
        for (XObject xObject : dataList) {
            Order order = (Order) xObject;
            orderList.add(order);
            dataResultList.add(new DataResult(true, "", xObject));
        }

        // 判断业务类型和订单状态
        Order order = orderList.get(0);
        // 只推送业务类型为 项目订单: 3309750193298054 预交付订单: 3309752318565039
        List<Long> includeTypeList = Arrays.asList(3309750193298054L, 3309752318565039L);
        LOGGER.info("订单归档时，推送订单数据到PMS entityType" + order.getEntityType());
        if (Objects.equals(order.getOrderStatus__c(), 2) && includeTypeList.contains(order.getEntityType())) {
            LOGGER.info("订单归档时，推送订单数据到PMS 开始推送");
            PushOrderToPms pushOrderToPms = new PushOrderToPms();
            pushOrderToPms.pushPmsOrderData(order.getId());
        }
        LOGGER.info("订单归档时，推送订单数据到PMS 触发结束");
        return new TriggerResponse(true, "", dataResultList);
    }

}
